/* 暂时只实现同步get请求 */
$.sync = function(url, callback, dataType, timeout) {
    sync_rst = false;
    if (dataType == undefined) dataType = 'text';
    _obj = {
        'url': url,
        'async': false,
        'dataType': dataType,
        'success': function(data) {
            sync_rst = callback(data);
        },
        'error': function(jqXHR, textStatus, errorThrown) {
            sync_rst = callback({
                'xhr': jqXHR,
                'status': textStatus,
                'error': errorThrown
            });
        }
    };

    if (timeout != undefined) {
        _obj.timeout = timeout;
    }

    $.ajax(_obj);
    return sync_rst;
}

///使用name方法跨域请求
$.csName = function(url, callback) {
    _iframe = $('<iframe/>');
    _iframe.attr('src', url);
    _iframe.css('display', 'none');
    _iframe.ready(function() {
        _iframe.attr('src', 'about:blank');
        _iframe.ready(function() {
            callback(_iframe.contentWindow.name);
        });
    });
    $('body').append(_iframe);
}
///使用iframe，对跨域地址进行请求，不对结果做处理
$.csRequire=function(url,querys,method){
    if(!method) method=='get';
    _form=$('<form/>');
    _form.attr('method',method).attr('action',url).css('display','none');
    for(var q in querys){
        _ele=$('<input/>').attr('type','text').val(querys[q]).attr('name',q);
        _form.append(_ele);
    }
    $('body').append(_form);
    _form.submit();
}


jQuery.fn.extend({
    dom: function() {
        _doms = [];
        $(this).each(function(index, el) {
            _doms.push(el);
        });
        return _doms;
    },
    //窗口可以通过鼠标和键盘移动
    /* handleClass: 可以使用手柄移动的位置，全部可移动 */
    automove: function(handleClass) {
        domhandle = (handleClass == undefined ? this : $(this).find('.' + handleClass));
        dom = this;
        paddingX = undefined;
        paddingY = undefined;
        var movehandler = function(e) {
            $(dom).css({
                'left': e.pageX - paddingX > 0 ? e.pageX - paddingX : 0,
                'top': e.pageY - paddingY > 0 ? e.pageY - paddingY : 0
            });
        } /* 存在一个小故障 */
        $(domhandle).mousedown(function(e) {
            paddingX = e.pageX - $(dom).offset().left;
            paddingY = e.pageY - $(dom).offset().top;
            $(dom).css('cursor', 'move');
            console.log($(dom).css('cursor'));
            $(domhandle).bind('mousemove', movehandler);
        });
        $(domhandle).mouseup(function(e) {
            $(dom).css('cursor', 'default');
            console.log($(dom).css('cursor'));
            $(domhandle).unbind('mousemove', movehandler);
        });
        $(domhandle).mouseout(function(e) {
            $(dom).css('cursor', 'default');
            console.log($(dom).css('cursor'));
            $(domhandle).unbind('mousemove', movehandler);
        });

    },
    ///自适应位置
    autopos: function(pos) {
        //{left:xx,top:xx}
        //middle:{left:auto,top:auto}
        //{right:xx;bottom:xx}
        if (pos['left'] === undefined && pos['right'] === undefined) {
            pos['left'] = 'auto';
        }
        if (pos['top'] === undefined && pos['bottom'] === undefined) {
            pos['top'] = 'auto';
        }
        css = {};
        if (pos['left'] == 'auto' || pos['right'] == 'auto') {
            css['left'] = ($(window).width() - $(dom).outerWidth()) / 2;
        } else {
            if (pos['left'] === undefined) {
                css['right'] = pos['right'];
            } else if (pos['right'] === undefined) {
                css['left'] = pos['left'];
            }
        }
        if (pos['top'] == 'auto' || pos['bottom'] == 'auto') {
            css['top'] = ($(window).height() - $(dom).outerHeight()) / 2;
        } else {
            if (pos['top'] === undefined) {
                css['bottom'] = pos['bottom'];
            } else if (pos['bottom'] === undefined) {
                css['top'] = pos['top'];
            }
        }
        dom = this;
        $(window).resize(function() {
            $(dom).css(css);
        });
    },
    ///提示框
    title: function() {
        $(this).each(function(index, el) {
            _title = $(el).attr('advanced-title');
            if (_title) {
                _box = $('<div class="ct-title-box"/>');
                $('body').append(_box);;
                $(el).mouseenter(function(e) {
                    _offsetX = $(this).offset().left;
                    _offsetY = $(this).offset().top;
                    _title = $(this).attr('advanced-title');
                    _box.empty().append($('<p>' + _title + '</p>'));
                    _boxHeight = _box.outerHeight();
                    _boxWidth = _box.outerWidth();
                    _elWidth = $(this).outerWidth();
                    _elHeight = $(this).outerHeight();
                    _box.css({
                        'left': ((_x = _offsetX - (_boxWidth - _elWidth) / 2) > 0) ? _x : 0,
                        'top': (_y = _offsetY - _boxHeight) > 0 ? _y : _elHeight + _offsetY
                    });
                    _box.show();
                });
                $(el).mouseout(function(e) {
                    _boxOffsetXMin = _box.offset().left;
                    _boxOffsetYMin = _box.offset().top;
                    _boxOffsetXMax = _box.outerWidth() + _boxOffsetXMin;
                    _boxOffsetYMax = _box.outerHeight() + _boxOffsetYMin;
                    if (!Matrix. in ([e.pageX, e.pageY], _boxOffsetXMin, _boxOffsetXMax, _boxOffsetYMin, _boxOffsetYMax)) {
                        _box.hide();
                    }
                });
                _box.mouseleave(function(e) {
                    _boxOffsetXMin = $(el).offset().left;
                    _boxOffsetYMin = $(el).offset().top;
                    _boxOffsetXMax = $(el).outerWidth() + _boxOffsetXMin;
                    _boxOffsetYMax = $(el).outerHeight() + _boxOffsetYMin;
                    if (!Matrix. in ([e.pageX + 1, e.pageY + 1], _boxOffsetXMin, _boxOffsetXMax, _boxOffsetYMin, _boxOffsetYMax)) {
                        _box.hide();
                    }
                    //_box.hide();
                });


            }
        });
    }
});